package com.h2n.framework.composer;

import org.zkoss.zul.*;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zkplus.spring.SpringUtil;
import com.h2n.framework.dao.IModuleDAO;
import com.h2n.framework.dto.ModuleDTO;

import java.util.List;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: NhatNV
 * Date: Jul 30, 2009
 * Time: 12:47:54 AM
 * To change this template use File | Settings | File Templates.
 */
public class MasterPageComposer extends AbstractPageComposer{
  Tabs tabsMain;
  Tabpanels tabpanelsMain;
  Toolbarbutton btnLogout_MasterPage;
  Label lblHeaderTitle_MasterPage;

  public void doAfterCompose(Component comp)throws Exception{
    super.doAfterCompose(comp);
    if(currentMemberLogined == null){
      Executions.sendRedirect(PageConstantsFramework.URL_LOGIN);
    }
    else{
      if(tabsMain.getChildren().size() <= 0){
        createMainMenu();
      }
    } 
  }

  public void onClick$btnLogout_MasterPage(Event e)throws Exception{
    session.removeAttribute(PageConstantsFramework.SESSION_MEMBER_LOGINED);
    Executions.sendRedirect(PageConstantsFramework.URL_LOGIN);
  }

  public void createMainMenu() throws Exception {
    IModuleDAO imodule = (IModuleDAO) SpringUtil.getBean("iModuleDao");
    List<ModuleDTO> liMod = imodule.getParentByMemberId(currentMemberLogined.getId());
    if(liMod != null && liMod.size() > 0){
      Iterator it = liMod.iterator();
      while(it.hasNext()){
        ModuleDTO moDto = (ModuleDTO)it.next();
        Tab tab = new Tab();
        tab.setId(moDto.getId()+"");
        tab.setLabel(moDto.getModule_Name());        
//        tab.setWidth("70px");
        tabsMain.appendChild(tab);
        String url = moDto.getClass_Path();
        if(url != null && !url.trim().equals("")){
          url += url.indexOf("&") == -1?"?" + PageConstantsFramework.PARAMETER_PARENT_ID + "=" + moDto.getId():"&"+PageConstantsFramework.PARAMETER_PARENT_ID + "=" + moDto.getId();
        }
        Include include = new Include(url);
        include.setStyle("width:100%;height:100%");
        Tabpanel tabPanel = new Tabpanel();
        tabPanel.appendChild(include);
        tabpanelsMain.appendChild(tabPanel);
      }
    }
  }
}
